import React, { Component } from 'react';
import { connect } from 'dva';
import { message, Modal, Pagination, Popconfirm, Button } from 'antd';
import styles from './../Users.css';
import moment from 'moment';
import Factory from '../../factory';
import * as datetime from '../../../utils/datetime'
import dataEditCreate from '../../addons/DataEdit'
import dataTableCreate from '../../addons/DataTable'
import dataImportCreate from '../../addons/DataImport'
import * as menuService from '../../../services/menus';
import numeral from 'numeral';
const daoName = "CompZSBL";
const ButtonGroup = Button.Group;
const renderNumber = (num) => {
  let value = ''
  if (num) {
    value = numeral(num).format('0,0.00')
  }
  return value
};
const columns = [  

    { title: '记账月份', width: 220, sorter: true, dataIndex: "JZYF", key: 'JZYF', dispaly: false, filter: true, export: true },
    { title: '分类', width: 180, sorter: true, dataIndex: 'FL', key: 'FL', dispaly: true, filter: true, export: true },
    // { title: '导入日期', width: 110, sorter: true, key: 'DTID', render: data => datetime.format(data.DTID, "yyyy-MM-dd"), dispaly: true, filter: true, export: true },
    { title: '客户名称', nowrap: true, key: 'KHMC', caption: "KHMC", width: 250, sorter: true, dispaly: true, filter: true, export: true  },
    { title: '交易日期', width: 220, sorter: true, dataIndex: "JYRQ", key: 'JYRQ', dispaly: false, filter: true, export: true },
    { title: '中收科目号', width: 200, sorter: true, dataIndex: 'ZSKMH', key: 'ZSKMH', dispaly: true, filter: true, export: true },
    { title: '费用名称', width: 300, sorter: true, dataIndex: 'FYNM', key: 'FYNM', dispaly: true, filter: true, export: true },
    { title: '中收金额', width: 120, sorter: true, dataIndex: "ZSJE", key: 'ZSJE', dispaly: true, filter: true, export: true },
    { title: '经营单位', width: 180, sorter: true, dataIndex: 'JJDW', key: 'JJDW', dispaly: true, filter: true, export: true },

    { title: '员工号1', dataIndex: 'EMNO1',  key: 'EMNO1', width: 110, sorter: true, dispaly: true, filter: true, export: true  },
    { title: '金额1', width: 120, sorter: true, dataIndex: "JE1", key: 'JE1', dispaly: true, filter: true, export: true },
    { title: '员工号2', dataIndex: 'EMNO2', key: 'EMNO2', width: 110, sorter: true, dispaly: true, filter: true, export: true  },
    { title: '金额2', width: 120, sorter: true, dataIndex: "JE2", key: 'JE2', dispaly: true, filter: true, export: true },
    { title: '员工号3', dataIndex: 'EMNO3', key: 'EMNO3', width: 110, sorter: true, dispaly: true, filter: true, export: true  },
    { title: '金额3', width: 120, sorter: true, dataIndex: "JE3", key: 'JE3', dispaly: true, filter: true, export: true },
    { title: '员工号4', dataIndex: 'EMNO4', key: 'EMNO4', width: 110, sorter: true, dispaly: true, filter: true, export: true  },
    { title: '金额4', width: 120, sorter: true, dataIndex: "JE4", key: 'JE4', dispaly: true, filter: true, export: true },
    { title: '员工号5', dataIndex: 'EMNO5',key: 'EMNO5', width: 110, sorter: true, dispaly: true, filter: true, export: true  },
    { title: '金额5', width: 120, sorter: true, dataIndex: "JE5", key: 'JE5', dispaly: true, filter: true, export: true },
    { title: '员工号6', dataIndex: 'EMNO6',key: 'EMNO6', width: 110, sorter: true, dispaly: true, filter: true, export: true  },
    { title: '金额6', width: 120, sorter: true, dataIndex: "JE6", key: 'JE6', dispaly: true, filter: true, export: true },
];
const controls = [
    // { name: 'DTID', dataType: "date", type: "date", label: '导入日期', grid: 12 / 24 },
    { name: 'JZYF', type: 'month',dataType: "month", label: '记账月份', grid: 12 / 24 },
    { name: 'FL', type: 'text', label: '分类', grid: 12 / 24 },
    { name: 'KHMC', type: 'text', label: '客户名称', grid: 12 / 24 },
    { name: 'JYRQ', type: 'text', label: '交易日期', grid: 12 / 24 },
    { name: 'ZSKMH', type: 'text', label: '中收科目号', grid: 12 / 24 },
    { name: 'FYNM', type: 'text', label: '费用名称', grid: 12 / 24 },
    { name: 'ZSJE', type: "number", dataType: "number", label: '中收金额(元)', grid: 12 / 24 },
    { name: 'JJDW', type: 'text', label: '经营单位', grid: 12 / 24 },

    { name: "EMNO1", label: "员工1", type: "text",required: true,  grid: 12 / 24},
    { name: 'JE1', type: 'text', label: '金额1',required: true,  grid: 12 / 24 },
  
    { name: "EMNO2", label: "员工2", type: "text", grid: 12 / 24},
    { name: 'JE2', type: 'text', label: '金额2',grid: 12 / 24 },
   
    { name: "EMNO3", label: "员工3", type: "text",  grid: 12 / 24},
    { name: 'JE3', type: 'text', label: '金额3', grid: 12 / 24 },
  
    { name: "EMNO4", label: "员工4", type: "text", grid: 12 / 24},
    { name: 'JE4', type: 'text', label: '金额4', grid: 12 / 24 },
   
    { name: "EMNO5", label: "员工5", type: "text", grid: 12 / 24},
    { name: 'JE5', type: 'text', label: '金额5', grid: 12 / 24 },
  
    { name: "EMNO6", label: "员工6", type: "text",  grid: 12 / 24},
    { name: 'JE6', type: 'text', label: '金额6', grid: 12 / 24 },

];
const EditFrom = dataEditCreate(daoName);
const DataTable = dataTableCreate(daoName);
const DataImport = dataImportCreate(daoName);
class ZSBLDRForm extends Component {
  constructor(props) {
    super(props);
    this.state = {
      dataChange: false
    };
  }
  componentDidMount() {
    this.props.dispatch({
      type: `${daoName}/setKeyName`,
      payload: { keyName: "ID", permID: this.props.permID }
    });
    this.props.dispatch({
      type: `${daoName}/fetch`,
      payload: { daoName, page: 1, pageSize: 20 }
    });
    this.loadOptionData();
  }
  componentWillReceiveProps(nextProps) {
    const { showImport } = this.props
    if (!nextProps.showImport && showImport) {
      this.props.dispatch({
        type: `${daoName}/fetch`,
        payload: { daoName, page: 1, pageSize: 20 }
      });
    }
  }
  loadOptionData() {
    let options = []
    controls.map(c => {
      if (c.fetch) {
        let f = c.fetch;
        if (c.type === 'cascader-select') {
          f.isCascader = true;
        }
        if (c.type !== 'search') {
          options.push(f);
        }
      }
    })
    if (daoName && options.length > 0) {
      this.props.dispatch({ type: `${daoName}/fetchOptions`, payload: { options } });
    }
  }
  showImportHandler() {
    this.props.dispatch({
      type: `${daoName}_DataImport/setDisplay`,
      payload: { display: true },
    });
  }
  DownTmportHandler() {
    let columnHeaders = []
    columns.map((c, j) => {
      const name = c.dataIndex ? c.dataIndex : c.key
      if (!c.export) {
        return
      }
      if (!name||name=="JZYF") {
        return;
      }
      let header = {
        Name:name,
        Caption: c.title,
        Width: c.width
      }
      columnHeaders.push(header)
    })
    let form = document.createElement('form');//定义一个form表单
    let whereItems = [];
    whereItems.push({
      FieldName: "1",
      Operator: "<>",
      FieldValues: "1"
    })
    const whereCauses = []
    whereCauses.push({
      WhereItems: whereItems,
      RelationSign: "AND"
    })
    const orderItems = []
    form.style = "display:none";
    form.target = "_blank";
    form.method = "post";
    form.action = "/api/ExportXlsx/" + daoName;
    let input1 = document.createElement("input");
    input1.type = "hidden";
    input1.name = "WhereCauses";
    input1.value = JSON.stringify(whereCauses);
    let input2 = document.createElement("input");
    input2.type = "hidden";
    input2.name = "OrderItems";
    input2.value = JSON.stringify(orderItems)
    let input3 = document.createElement("input");
    input3.type = "hidden";
    input3.name = "ColumnHeaders";
    input3.value = JSON.stringify(columnHeaders);
    let input4 = document.createElement("input");
    input4.type = "hidden";
    input4.name = "PermID";
    input4.value = "" + this.props.permID;
    let input5 = document.createElement("input");
    input5.type = "hidden";
    input5.name = "UserID";
    input5.value = "" + this.props.userID;
    document.body.appendChild(form);//将表单放置在web中
    form.appendChild(input1);
    form.appendChild(input2);
    form.appendChild(input3);
    form.appendChild(input4);
    form.appendChild(input5);
    form.submit();
    document.body.removeChild(form)
  }
  render() {
    let { showEdit, record, showImport, permID, popedomTypes } = this.props
    const pTypes = popedomTypes[permID]
    let data = null
    if (pTypes.indexOf("2") !== -1) {
      data = <ButtonGroup style={{ zIndex: 0 }}>
        <Button onClick={this.showImportHandler.bind(this)}>导入</Button>
        <Button onClick={this.DownTmportHandler.bind(this)}>下载导入模板</Button>
      </ButtonGroup>
    }
    return (
      <div className={styles.normal} style={{ background: '#fff', marginTop: -16, padding: '5px 5px' }}>
        <div>
          <div className={styles.create}>
            {data}
          </div>
          <DataTable ref="CompZSBL" daoName={daoName} columns={columns} controls={controls} permID={permID} />
          <DataImport title="中收补录" importName="CompZSBL" daoName={daoName} keys={["ID"]} paramNames={["JZYF"]} importMode="DeleteAfterInsert" controls={controls} forbidConvert={true} saveByDao={true} />
        </div>
      </div>
    )
  }
}

function mapStateToProps(state) {
  const { showEdit, record, selectedRowKeys, keyName } = state[daoName];
  const { userID, orgID, pOrgID, popedomTypes } = state.menus;
  const { display } = state[`${daoName}_DataImport`];
  return {
    userID,
    orgID,
    pOrgID,
    popedomTypes,
    showEdit,
    selectedRowKeys,
    keyName,
    record,
    showImport: display
  }
}
ZSBLDRForm = connect(mapStateToProps)(ZSBLDRForm)
export default ZSBLDRForm;

let render = (props) => React.createElement(ZSBLDRForm, props);
let f = new Factory()
f.set("ZSBLDRForm", render)
